package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.ShRepairMaterialList;
import com.jy.adscm.operation.aftersales.bean.ShRepairMaterialListCheck;
import com.jy.adscm.operation.aftersales.bean.ShylMaterial;
import com.jy.adscm.operation.aftersales.bean.ShylMaterialdetail;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairMaterialListCheckMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairMaterialListMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShylMaterialdetailMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairMaterialListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.aftersales.service.ShylMaterialService;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.JqylSettingMaterialCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylSettingMaterialCheckMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-22
 */
@Service
public class ShRepairMaterialListServiceImpl extends ServiceImpl<ShRepairMaterialListMapper, ShRepairMaterialList> implements ShRepairMaterialListService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    CgMaterialMapper cgMaterialMapper;
     @Autowired
    ShylMaterialService shylMaterialService;
     @Autowired
    ShylMaterialdetailMapper shylMaterialdetailMapper;
     @Autowired
    JqylSettingMaterialCheckMapper jqylSettingMaterialCheckMapper;
     @Autowired
    ShRepairMaterialListCheckMapper shRepairMaterialListCheckMapper;
     @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertShRepairMaterialList(ShRepairMaterialList shRepairMaterialList) throws AdscmSystemException {

        //查询该物料是否有编码
         CgMaterial cgMaterial = cgMaterialMapper.selectById(shRepairMaterialList.getName());
         if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCode(),null)){
             String code="s"+cgMaterial.getCode()+  new SimpleDateFormat("yyMMdd").format(new Date());
             //查询是否有更大的编码
             Wrapper<ShRepairMaterialList> shRepairMaterialListWrapper=new EntityWrapper<>();
             shRepairMaterialListWrapper.like("code",code, SqlLike.RIGHT);
             shRepairMaterialListWrapper.last("limit 1");
             List<ShRepairMaterialList> shRepairMaterialLists = super.selectList(shRepairMaterialListWrapper);
             if (shRepairMaterialLists!=null &&shRepairMaterialLists.size()>0){
                 ShRepairMaterialList shRepairMaterialList1 = shRepairMaterialLists.get(0);
                  code =code+String.valueOf(Integer.valueOf( shRepairMaterialList1.getCode().replace(code,"")) +1)  ;
             }else {
                 code=code+"01";
             }

             shRepairMaterialList.setCode(code);
         }

         boolean insert = super.insert(shRepairMaterialList);
        //减库存
        ShylMaterial shylMaterial = shylMaterialService.updateShylMaterialValue(shRepairMaterialList.getKid().toString(), -shRepairMaterialList.getNum());
        //添加出库记录
        ShylMaterialdetail  shylMaterialdetail =new ShylMaterialdetail();
        shylMaterialdetail.setOutstoragetime(new Date());
        shylMaterialdetail.setTid(shylMaterial.getTid().toString());
        shylMaterialdetail.setName(shylMaterial.getResourcename());
        shylMaterialdetail.setUseperson(shRepairMaterialList.getCreator());
        shylMaterialdetail.setUsenums(shRepairMaterialList.getNum().doubleValue());
        shylMaterialdetail.setMaterialstate("1");
        shylMaterialdetail.setBatchnums(shylMaterial.getBatchnums());
        shylMaterialdetail.setPid(shRepairMaterialList.getPid().toString());
        shylMaterialdetailMapper.insert(shylMaterialdetail);
        //查询如果有检验记录生成对应的检验记录
         Wrapper<JqylSettingMaterialCheck> jqylSettingMaterialCheckWrapper=new EntityWrapper<>();
         jqylSettingMaterialCheckWrapper.eq("pid",shRepairMaterialList.getName());
         List<JqylSettingMaterialCheck> jqylSettingMaterialChecks = jqylSettingMaterialCheckMapper.selectList(jqylSettingMaterialCheckWrapper);
         if (jqylSettingMaterialChecks!=null&&jqylSettingMaterialChecks.size()>0){
             for (JqylSettingMaterialCheck jqylSettingMaterialCheck : jqylSettingMaterialChecks) {
                 ShRepairMaterialListCheck shRepairMaterialListCheck=new ShRepairMaterialListCheck();
                 shRepairMaterialListCheck.setPid(shRepairMaterialList.getId());
                 shRepairMaterialListCheck.setCid(jqylSettingMaterialCheck.getId());
                 shRepairMaterialListCheck.setName(jqylSettingMaterialCheck.getName());
                 shRepairMaterialListCheck.setType(jqylSettingMaterialCheck.getType());
                 shRepairMaterialListCheck.setUnit(jqylSettingMaterialCheck.getUnit());
                 shRepairMaterialListCheck.setRemark(jqylSettingMaterialCheck.getRemark());
                 shRepairMaterialListCheck.setReference(jqylSettingMaterialCheck.getReference());
                 shRepairMaterialListCheck.setRank(jqylSettingMaterialCheck.getRank());
                 shRepairMaterialListCheckMapper.insert(shRepairMaterialListCheck);
             }
         }else {
             //免检物料
             shRepairMaterialList.setState("1");
             shRepairMaterialList.setIsCheck(1);
         }

         return insert;
    }

    @Override
    public boolean deleteShRepairMaterialList(Integer id, Token token)throws AdscmSystemException {
        ShRepairMaterialList shRepairMaterialList = super.selectById(id);
        //恢复库存
        ShylMaterial shylMaterial = shylMaterialService.updateShylMaterialValue(shRepairMaterialList.getKid().toString(), shRepairMaterialList.getNum());
        //删除出库记录
        Wrapper<ShylMaterialdetail> wrapper=new EntityWrapper<>();
        wrapper.eq("tid",shylMaterial.getTid());
        wrapper.eq("pid",shRepairMaterialList.getPid());
         shylMaterialdetailMapper.delete(wrapper);
         Wrapper<ShRepairMaterialListCheck> shRepairMaterialListCheckWrapper=new EntityWrapper<>();
        shRepairMaterialListCheckWrapper.eq("pid",id);
        List<ShRepairMaterialListCheck> shRepairMaterialListChecks = shRepairMaterialListCheckMapper.selectList(shRepairMaterialListCheckWrapper);
        if (shRepairMaterialListChecks!=null&& shRepairMaterialListChecks.size()>0){
            for (ShRepairMaterialListCheck shRepairMaterialListCheck : shRepairMaterialListChecks) {
                shRepairMaterialListCheckMapper.deleteById(shRepairMaterialListCheck.getId());
            }
        }
        boolean b = super.deleteById(id);
        return b;
    }

    @Override
    public List<ShRepairMaterialList> selectBypid(Integer pid) {
         Wrapper<ShRepairMaterialList> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        List<ShRepairMaterialList> shRepairMaterialLists = super.selectList(wrapper);
        for (ShRepairMaterialList shRepairMaterialList : shRepairMaterialLists) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairMaterialList.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(shRepairMaterialList.getName());
                shRepairMaterialList.setNameName(cgMaterial.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairMaterialList.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairMaterialList.getCreator());
                shRepairMaterialList.setCreatorName(jyUser.getName());
            }
        }
        return shRepairMaterialLists;
    }
}
